﻿using System.Collections.Generic;
using System.Linq;
using NetSurveillance.WebApp.DataServices.Models;
using NetSurveillance.WebApp.Models;

namespace NetSurveillance.WebApp.Utilities
{
    public class ZTreeUtility
    {
        public static IList<ZTreeNode> GenerateHostNodes(IEnumerable<Host> hosts,
                                                         ICollection<long> selectHostIds)
        {
            var hostNodes = new List<ZTreeNode>();
            foreach (var hostGroup in hosts.GroupBy(p => p.Mac))
            {
                var hostMacNode = new ZTreeNode
                    {
                        Name = hostGroup.Key,
                        IsParent = true,
                        Children = new List<ZTreeNode>()
                    };
                foreach (var host in hostGroup)
                {
                    var hostNode = new ZTreeNode
                        {
                            Name = host.HostName,
                            Checked = selectHostIds != null && selectHostIds.Contains(host.Id),
                            Expandos = new { HostId = host.Id, Ip = host.IP, host.Mac }
                        };
                    hostMacNode.Children.Add(hostNode);
                }
                hostNodes.Add(hostMacNode);
            }

            return hostNodes;
        }
    }
}